/*
 * @Author: szx
 * @Date: 2022-04-21 12:58:27
 * @LastEditTime: 2022-04-21 13:11:16
 * @Description:
 * @FilePath: \leetcode\剑指offerII\075\075.js
 */
/**
 * @param {number[]} arr1
 * @param {number[]} arr2
 * @return {number[]}
 */
var relativeSortArray = function (arr1, arr2) {
    const map = new Map();
    for (let i of arr1) map.set(i, (map.get(i) || 0) + 1);
    const res = [];
    for (let i of arr2) {
        if (map.has(i)) {
            for (let j = map.get(i); j > 0; j--) {
                res.push(i);
            }
            map.delete(i);
        }
    }
    const last = [];
    for (let m of map) {
        for (let j = m[1]; j > 0; j--) {
            last.push(m[0]);
        }
    }
    return res.concat(last.sort((a, b) => a - b));
};
